package com.chengh.nami.web.controller.system;

import com.chengh.nami.common.response.R;
import com.chengh.nami.common.response.TreeSelect;
import com.chengh.nami.system.pojo.SysDept;
import com.chengh.nami.system.service.ISysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门类处理器
 *
 * @author chengh
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dept")
@Tag(name = "SysDeptController", description = "部门管理")
public class SysDeptController {
    private final ISysDeptService deptService;

    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list")
    @Operation(summary = "获取所有的部门信息", description = "需要权限system:dept:list")
    public R<List<SysDept>> list(SysDept sysDept) {
        List<SysDept> sysDeptList = deptService.getDeptList(sysDept);
        return R.ok(sysDeptList);
    }

    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @PostMapping("")
    @Operation(summary = "添加部门信息", description = "需要权限system:dept:add")
    public R<Void> addDept(@RequestBody SysDept sysDept) {
        boolean b = deptService.addSysDept(sysDept);
        return R.bool(b);
    }

    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/{id}")
    @Operation(summary = "查询部门信息", description = "需要权限system:dept:list")
    public R<SysDept> dept(@PathVariable Long id) {
        return R.ok(deptService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @PutMapping("")
    @Operation(summary = "修改部门信息", description = "需要权限system:dept:edit")
    public R<Void> editDept(@RequestBody SysDept sysDept) {
        boolean b = deptService.updateSysDept(sysDept);
        return R.bool(b);
    }

    @PreAuthorize("@ss.hasPermi('system:dept:del')")
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门信息", description = "需要权限system:dept:del")
    public R<Void> delDept(@PathVariable Long id) {
        boolean b = deptService.delDept(id);
        return R.bool(b);
    }

    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/deptTree")
    @Operation(summary = "获取部门列表")
    public R<List<TreeSelect>> deptTree(SysDept dept) {
        return R.ok(deptService.selectDeptTreeList(dept));
    }
}
